import React from "react";
import {createFromIconfontCN} from "@ant-design/icons";
import {Link} from "react-router-dom";
import {TreeDataNode} from "antd";
import {MenuTreeData} from "../type/entity/SecurityMenu";

const IconFont = createFromIconfontCN({
    scriptUrl: [
        'http://192.168.2.199:3269/icon/summer-icon.js',
        'http://192.168.2.199:3269/icon/summer-other-icon.js'
    ]
});

/**
 * 转换菜单的图标,从字符串转换为IconFont
 */
export const menuIconTransition =<T extends MenuTreeData> (menuData: T[]): T[] => {
    menuData.forEach( (m: any) => {
        // console.log("转换菜单的图标", m);
        if (typeof m.label === "string" && !m.children){
           m.label = <Link key={m.key} to={m.path}>{m.label}</Link>;
        }
        if (typeof m.icon === "string"){
            m.icon = <IconFont type={m.icon as string} />;
        }
        m.key = m.path;
        if (m.children){
            menuIconTransition(m.children);
        }
    });
    return menuData;
};

/**
 * 转换树图标,将string形式的图标转换为IconFont <br/>
 * 需要转换的数据不可以是const常量,因为const常量是只读的
 */
export const treeIconTransition = (data: TreeDataNode[]) => {
    data.forEach( m => {
        if (typeof m.icon === "string"){
            m.icon = <IconFont style={{fontSize: "16px"}} type={m.icon as string} />;
        }
        if (m.children){
            treeIconTransition(m.children);
        }
    });
};


export default IconFont;